local UIBase = require("view.UIBase")
local LoginView = class("LoginView", UIBase)
local UIUtil = require("utils.UIUtil")
local GameUtils = require("utils.GameUtils")

function LoginView:ctor()
    local resource = {
        path = "Prefabs/View",
        name = "LoginView",
        canvas = 2
    }
    LoginView.super.ctor(self, resource)
end

function LoginView:start()
    self:_initFixUI()
    self:_initListener()
end

function LoginView:_initListener()
    self._signalLoginSuccess =
        G_SignalManager:add(SignalConst.EVENT_USER_FLUSH_DATA, handler(self, self._onEventLoginSuccess))
    self._signalRegisterSuccess =
        G_SignalManager:add(SignalConst.EVENT_USER_REGISTER_SUCCESS, handler(self, self._onEventRegisterSuccess))
end

function LoginView:_initFixUI()
    self._btnLogin.onClick:AddListener(handler(self, self.onClickLigon))
    self._btnConnect.onClick:AddListener(handler(self, self.onClickConnect))
end

function LoginView:connectServer()
    G_SocketManager:addConnectCallback(
        handler(self, self._onConnectSuccess),
        handler(self, self._onConnectFail),
        handler(self, self._onnDisconnect)
    )
    G_SocketManager:connectServer()
end

function LoginView:_onConnectSuccess()
end

function LoginView:_onConnectFail()
end

function LoginView:_onnDisconnect()
end

function LoginView:onDestroy()
    self._signalLoginSuccess:remove()
    self._signalLoginSuccess = nil
    self._signalRegisterSuccess:remove()
    self._signalRegisterSuccess = nil
end

function LoginView:_onEventLoginSuccess(id, message)
    self:close()
    G_SceneManager:loadSceneAnsc(1)
end

function LoginView:_onEventRegisterSuccess(id, message)
end

function LoginView:onClickLigon()
    local userName = self._inputName.text
    printDebug(userName)
    if userName ~= "" then
        G_UserData:getBase():setName(userName)
    end
    if not G_SocketManager:isContenting() then
        self:close()
        G_SceneManager:loadSceneAnsc(1)
    end
end

function LoginView:onClickConnect()
    local userName = self._inputName.text
    printDebug(userName)
    if userName ~= "" then
        G_UserData:getBase():setName(userName)
    end
    if not G_SocketManager:isContenting() then
        self:connectServer()
    end
end

function LoginView:onDragBegin(event)
    printDebug(event.position)
end

function LoginView:onDragMove(event)
    printDebug(event.position)
end

function LoginView:onDragEnd(event)
    printDebug(event.position)
end

return LoginView
